//
//  TT_TabbarViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/4/1.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "TT_TabbarViewController.h"
#import "TT_NavigationViewController.h"

@interface TT_TabbarViewController ()<UINavigationControllerDelegate,UITabBarControllerDelegate>

@property (nonatomic, assign) NSInteger indexFlag;

@end

@implementation TT_TabbarViewController

+ (instancetype)shareTabbar {
    
    static TT_TabbarViewController *shareTab = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!shareTab) {
            shareTab = [[self alloc] init];
        }
    });
    return shareTab;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.delegate = self;
    [self uiConfig];

    self.tabBar.translucent = NO;
    [self.tabBar setBarTintColor:UIColor.whiteColor];
    
    //背景
    CGRect rect = CGRectMake(0, 0, TTScreenWidth, TTScreenHeight);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    if (@available(iOS 13.0, *)) {
       UITabBarAppearance *tabBarAppearance = [self.tabBar.standardAppearance copy];
       [tabBarAppearance setBackgroundImage:img];
       [tabBarAppearance setShadowColor:[UIColor clearColor]];
       [self.tabBar setStandardAppearance:tabBarAppearance];
    } else {
       [self.tabBar setBackgroundImage:img];
       [self.tabBar setShadowImage:img];
    }
    
    //字体颜色
    if (@available(iOS 13.0, *)) {
        UITabBar *tabBar = [UITabBar appearance];
        [tabBar setTintColor:UIColorHex(0xff4242)];
        [tabBar setUnselectedItemTintColor:UIColorHex(0x333333)];
    } else {
        UITabBarItem *tabBarItem = [UITabBarItem appearance];
        [tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: UIColorHex(0x333333)} forState:UIControlStateNormal];
        [tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: UIColorHex(0xff4242)}forState:UIControlStateSelected];
    }
}

- (void)setCurrentIndex:(TT_TabbarIndexType)indexType {
    
    [[TT_TabbarViewController shareTabbar] setSelectedIndex:indexType];
}

- (void)uiConfig
{
    _homeNav = [self viewControllerWithTabTitle:@"首页"
                                          image:@"home_nor"
                                  selectedImage:@"home_sel"
                                      className:@"HomeViewController"];

    _mineNav = [self viewControllerWithTabTitle:@"我的"
                                          image:@"mine_nor"
                                  selectedImage:@"mine_sel"
                                      className:@"MineViewController"];

    self.viewControllers = @[_homeNav, _mineNav];
}

//默认图片设置以及选中状态图片设置
- (TT_NavigationViewController *)viewControllerWithTabTitle:(NSString*)title image:(NSString *)image selectedImage:(NSString *)selectedImage className:(NSString *)className
{
    Class cls = NSClassFromString(className);
    UIViewController *viewController = [[cls alloc] init];
    
    UIImage *Image = [UIImage imageNamed:image];
    UIImage *ImageSel = [UIImage imageNamed:selectedImage];
    
    Image = [Image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    ImageSel = [ImageSel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:Image selectedImage:ImageSel];

    TT_NavigationViewController *navController = [[TT_NavigationViewController alloc] initWithRootViewController:viewController];
    navController.navigationBar.translucent = NO;
    navController.navigationBar.barTintColor = [UIColor whiteColor];
//    navController.delegate = self;
    return navController;

}

//根据颜色生成图片的函数

- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    if (!color || size.width <= 0 || size.height <= 0) return nil;
        CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
        UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetFillColorWithColor(context, color.CGColor);
        CGContextFillRect(context, rect);
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    return image;
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
        
    [Utilities tapticEngineWork];
}

@end
